package by.bucha.testproject.fragments;

import by.bucha.testproject.R;
import by.bucha.testproject.adapters.PeopleListAdapter;
import by.bucha.testproject.adapters.PeopleListAdapter.ViewHolder;
import by.bucha.testproject.content.contract.TaskContract;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class PersonalListFragment extends ListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {

	public PersonalListFragment() {
	}

	private PeopleListAdapter mAdapter;

	private static final int LOADER_ID = 0;

	public static final String DETAILS_BACKSTACK_NAME = "details";

	public static final String ADD_BACKSTACK_NAME = "add";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setHasOptionsMenu(true);

		mAdapter = new PeopleListAdapter(getActivity().getApplicationContext(),
				null);

		setListAdapter(mAdapter);

		getLoaderManager().initLoader(LOADER_ID, null,
				PersonalListFragment.this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_bar_menu_items_fragment_personal_list_item_add:
			changeFragmentToAddPerson();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		menu.clear();
		inflater.inflate(R.menu.action_bar_menu_items_fragment_personal_list,
				menu);
	}

	@Override
	public void onListItemClick(ListView listView, View view, int position,
			long id) {
		super.onListItemClick(listView, view, position, id);

		ViewHolder idHolder = (ViewHolder) view.getTag();
		int modelNumber = idHolder.getModelId();

		changeFragmentToLeaderDetails(modelNumber);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int paramInt, Bundle paramBundle) {

		return new CursorLoader(getActivity().getApplicationContext(),
				TaskContract.WorkerColumns.CONTENT_URI, null, null, null,
				TaskContract.WorkerColumns.NAME);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> paramLoader, final Cursor cursor) {
		if (cursor.getCount() != 0) {
			mAdapter.swapCursor(cursor);
		} else {

			String warning = getActivity()
					.getApplicationContext()
					.getResources()
					.getString(
							R.string.personal_list_fragment_strings_empty_cursor);
			Toast.makeText(getActivity().getApplicationContext(), warning,
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> cursor) {
		mAdapter.swapCursor(null);

	}

	private void changeFragmentToAddPerson() {

		AddPersonFragment newFragment = new AddPersonFragment();

		FragmentTransaction transaction = getActivity()
				.getSupportFragmentManager().beginTransaction();

		// Replace whatever is in the fragment_container view with this
		// fragment,
		// and add the transaction to the back stack so the user can navigate
		// back
		transaction.replace(R.id.activity_main_tab_tab_content, newFragment);
		transaction.addToBackStack(ADD_BACKSTACK_NAME);

		// Commit the transaction
		transaction.commit();
	}

	private void changeFragmentToLeaderDetails(int id) {

		// Create fragment and give it an argument specifying the article it
		// should show

		PersonDetailsFragment newFragment = new PersonDetailsFragment();

		Bundle arguments = new Bundle();

		arguments.putInt(PersonDetailsFragment.EXTRA_MODEL_ID, id);
		newFragment.setArguments(arguments);

		FragmentTransaction transaction = getActivity()
				.getSupportFragmentManager().beginTransaction();

		// Replace whatever is in the fragment_container view with this
		// fragment,
		// and add the transaction to the back stack so the user can navigate
		// back
		transaction.replace(R.id.activity_main_tab_tab_content, newFragment);
		transaction.addToBackStack(DETAILS_BACKSTACK_NAME);

		// Commit the transaction
		transaction.commit();
	}
}
